<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" th:href="@{/static/component/pear/css/pear.css}">
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form action="" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">资源名称</label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="fileRealName" placeholder="" type="text">
                </div>
                <label class="layui-form-label">文件名称</label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="fileName" placeholder="" type="text">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-filter="file-query" lay-submit>
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button class="pear-btn pear-btn-md" type="reset">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="file-table" lay-filter="file-table"></table>
    </div>
</div>

</body>

<script id="role-toolbar" type="text/html">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        上传
    </button>
    <button class="pear-btn pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script id="role-bar" type="text/html">
    <a class="pear-btn pear-btn-primary pear-btn-sm" lay-event="download">
        <i class="pear-icon pear-icon-direction-down"></i>
    </a>
    <button class="pear-btn pear-btn-group pear-btn-sm" lay-event="seacherdata">
        <i class="layui-inline layui-icon layui-icon-search"></i></button>
    <button class="pear-btn pear-btn-green pear-btn-sm" lay-event="share" id="share">
        <i class="layui-inline layui-icon layui-icon-file"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>

</script>

<script id="file-image" type="text/html">
    <a href="javascript:void(0);" class="pear-text">查看</a>
</script>

<script id="file-createTime" type="text/html">
    {{layui.util.toDateString(d.createTime,  'yyyy-MM-dd HH:mm:ss')}}
</script>

<script th:src="@{/static/admin/js/jquery.min.js}"></script>
<script th:src="@{/static/component/layui/layui.js}"></script>
<script th:src="@{/static/component/pear/pear.js}"></script>
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" th:src="@{/static/admin/js/clipboard.min.js}"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'popup', 'common','hash'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let popup = layui.popup;
        let common = layui.common;
        let hash = layui.hash;

        let MODULE_PATH = "/system/file/";

        let cols = [
            [
                {type: 'checkbox'},
                {title: '资源名称', field: 'fileRealName', align: 'center', width: 200},
                {title: '文件名称', field: 'fileName', align: 'center'},
                {title: '文件路径', field: 'filePath', align: 'center'},
                {title: '类型', field: 'fileType', align: 'center', width: 80},
                {title: '文件大小', field: 'fileSize', align: 'center', width: 120},
                {title: '操作', toolbar: '#role-bar', align: 'center', width: 200}
            ]
        ]

        table.render({
            elem: '#file-table',
            url: MODULE_PATH + 'data',
            method:'post',
            page: true,
            limits: [10,15,20],
            limit: 10, //每页默认显示的数量
            cols: cols,
            skin: 'line',
            // height: 'full-148',
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            parseData:function(res){
                return {
                    "code" : res.code, //解析接口状态
                    "msg" : res.msg, //解析提示文本
                    "data" : res.data.records, //解析数据列表
                    "count" : res.data.total
                }
            }
        });

        table.on('tool(file-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'seacherdata') {
                window.show(obj);
            }else if (obj.event === 'download') {
                window.download(obj);
            }
            else if (obj.event === 'share') {
                window.share(obj);
            }
        });

        table.on('toolbar(file-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(file-query)', function (data) {
            table.reload('file-table', {where: data.field})
            return false;
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '上传',
                shade: 0.1,
                area: ['500px', '400px'],
                content: MODULE_PATH + 'add'
            });
        }
        window.download = function (obj) {
            // console.log(obj.data)fileRealName
            url=MODULE_PATH+"download"+"/"+obj.data.fileName+"/"+obj.data.fileType+"/"+obj.data.fileRealName
            window.open(url);
        }
        window.share = function (obj) {
            var type = obj.data.fileType;
            var name =obj.data.fileName;
            var file;
            if (type !==""){
                file = name+"."+type;
            }else {
                file = name;
            }
            var url = window.location.protocol+'//'+window.location.host+'/static/file/'+file; //要预览文件的访问地址
            var clipboard = new ClipboardJS('#share', {
                text: function () {
                    return url;
                }
            });
            clipboard.on('success', function (e) {
                alert("复制成功！");
            });
            clipboard.on('error', function (e) {
                alert("复制错误，请手动复制！");
            });


        }
        window.show = function (obj) {
            // layer.open()
            var type = obj.data.fileType;
            var name =obj.data.fileName;
            var file;
            if (type !==""){
                file = name+"."+type;
            }else {
                file = name;
            }
            var url = window.location.protocol+'//'+window.location.host+'/static/file/'+file; //要预览文件的访问地址
            window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(hash.Base64Encode(url)));
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该文件', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH+"remove",
                    dataType: 'json',
                    type: 'post',
                    data:JSON.stringify({"id":obj.data['id']}),
                    contentType:'application/json',
                    success: function(result) {
                        layer.close(loading);
                        if (result.code ===0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            // console.log(common.checkField(obj, 'id'))
            let ids = common.checkField(obj, 'id');
            if (ids==="") {
                popup.warning("未选中数据");
                return false;
            }
            layer.confirm('确定要删除选中文件', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/system/file/remove",
                    dataType: 'json',
                    type: 'post',
                    data:JSON.stringify({"id":ids}),
                    contentType:'application/json',
                    success: function(result) {
                        layer.close(loading);
                        if (result.code ===0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                table.reload('file-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function () {
            table.reload('file-table');
        }
    })
</script>
</html>